<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage helper
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: helper/network.php 1.0 2010-12-25 19:24 hongbin.hsu $
 */

// ------------------------------------------------------------------------
/**
 * 取客户端IP地址
 * @param string $proxy_override 代理覆盖
 * @return string
 */
if (!function_exists('get_client_ip'))
{
	function get_client_ip($proxy_override = true)
	{
		if ($proxy_override) {
			/* 优先从代理那获取地址或者 HTTP_CLIENT_IP 没有值 */
			$ip = empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_CLIENT_IP'] : $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else {
			/* 取 HTTP_CLIENT_IP, 虽然这个值可以被伪造, 但被伪造之后 NS 会把客户端真实的 IP 附加在后面 */
			$ip = empty($_SERVER['HTTP_CLIENT_IP']) ? NULL : $_SERVER['HTTP_CLIENT_IP'];
		}
		if (empty($ip)) { $ip = $_SERVER['REMOTE_ADDR']; }
		/* 真实的IP在以逗号分隔的最后一个, 当然如果没用代理, 没伪造IP, 就没有逗号分离的IP */
		if ($p = strrpos($ip, ',')) { $ip = substr($ip, $p+1);}
		
		return trim($ip);
	}
}

// ------------------------------------------------------------------------
/**
 * 文件头输出缓存
 * @param int $maxAge HTTP缓存时间
 * @param int $expire 失效时间
 * @return void
 */
 if (!function_exists('header_cache'))
{
    function header_cache($maxAge = 600,$expire = 25200)
    {
	    $gmt = gmmktime();
	    header("Cache-Control: max-age=".$maxAge);
	    header("Pragma: cache");
	    header("Expires: ". gmdate("D, d M Y H:i:s", $gmt + $expire) . " GMT");
    }
}
